import 'dart:ui';

import 'package:flutter/material.dart';
import 'package:magic_car/UI/update_dialog.dart';
import 'package:magic_car/UI/webview.dart';
import 'package:magic_car/jiance/pre_test_report.dart';
import 'package:magic_car/res/colors.dart';
import 'package:magic_car/user/userPrivate.dart';
import 'package:magic_car/util/HttpHelper.dart';
import 'package:magic_car/util/config.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:magic_car/main.dart';
import 'package:package_info/package_info.dart';
import 'package:toast/toast.dart';
import 'package:url_launcher/url_launcher.dart';
import 'dart:io';

class SetPage extends StatefulWidget {
  SetPage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<SetPage> {
  String version = 'v1.0.0';
  String localVersion = "v1.0.0";
  initState() {
    super.initState();
    getversion();
    getlocaversion();
    //  startLocations();//获取位置
  }

  getlocaversion() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    String localVersions = packageInfo.version;
    setState(() {
      localVersion = localVersions;
    });
    print("本地版本：${localVersions}");
  }

  showupdates(param) {
    showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return new Container(
          height: 300.0,
          child: new ListView(
            children: <Widget>[
              Container(
                  height: 40,
                  child: Padding(
                      padding: new EdgeInsets.fromLTRB(0, 0, 0, 0),
                      child: Center(child: Text('版本更新')))),
              Container(
                  height: 40,
                  child: Padding(
                      padding: new EdgeInsets.fromLTRB(0, 0, 0, 0),
                      child: Center(child: Text('新版本更新内容')))),
              Container(
                  height: 130,
                  child: Padding(
                      padding: new EdgeInsets.fromLTRB(0, 0, 0, 0),
                      child: Center(
                          child: Text(
                        param["ModifyContent"],
                        style: TextStyle(fontSize: 13),
                      )))),
              Padding(
                  padding: new EdgeInsets.fromLTRB(0, 10, 0, 0),
                  child: Container(
                    height: 40,
                    child: GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {
                        launch(param["DownloadUrl"]);
                        Navigator.pop(context);
                      },
                      child: Center(
                          child: Text('立即更新',
                              style: TextStyle(color: Colors.white))),
                    ),
                    decoration: new BoxDecoration(
                      border: new Border.all(
                          width: 1.0, color: Color.fromRGBO(225, 225, 225, 1)),
                      color: Colors.blue,
                    ),
                  )),
              Container(
                height: 40,
                child: GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {
                    Navigator.pop(context);
                  },
                  child: Center(
                      child: Text(
                    '暂不更新',
                    style: TextStyle(color: Colors.black),
                  )),
                ),
                decoration: new BoxDecoration(
                  border: new Border.all(
                      width: 1.0, color: Color.fromRGBO(225, 225, 225, 1)),
                  color: Color.fromRGBO(225, 225, 225, 1),
                ),
              )
            ],
          ),
        );
      },
    ).then((val) {
      print(val);
    });
  }

  void _showUpdateDialog(content, url) {
    showDialog<void>(
        context: context,
        barrierDismissible: false,
        builder: (_) => UpdateDialog(content, url));
  }

  checkversion() {
    if (Platform.isAndroid) {
      //SharedPreferences pref = await SharedPreferences.getInstance();
      Map<String, String> map = new Map();
      // map["id"] = pref.getString('uid');

      HttpHelper.post(HttpHelper.API_Version, (data) async {
        //loadpage.close();
        if (data != null) {
          print(data);

          PackageInfo packageInfo = await PackageInfo.fromPlatform();
          String localVersions = packageInfo.version;
          String romot_versionss = data['VersionName'];
          if (localVersions != romot_versionss) {
            _showUpdateDialog(data['ModifyContent'], data['DownloadUrl']);
          } else {
            Toast.show("目前已经是最新版本", context);
          }
        }
      }, params: map);
    }
    //SharedPreferences pref = await SharedPreferences.getInstance();
    // Map<String, String> map = new Map();
    // // map["id"] = pref.getString('uid');

    // HttpHelper.post(HttpHelper.API_Version, (data) async {
    //   //loadpage.close();
    //   if (data != null) {
    //     print(data);

    //     // PackageInfo packageInfo = await PackageInfo.fromPlatform();
    //     // String localVersion = packageInfo.version;
    //     String romot_version = data['VersionName'];
    //     if (localVersion != romot_version) {
    //        if(Platform.isAndroid){
    //       showupdates(data);
    //        }else{
    //          launch('https://apps.apple.com/cn/app/%E8%BD%A6%E9%AD%94%E4%BC%B0%E4%BC%97%E5%8C%85/id1502004468');
    //        }
    //     }else{
    //       Toast.show('目前已经是最新版本', context);
    //     }
    //   }
    // }, params: map);
  }

  getversion() async {
    Map<String, String> map = new Map();
    // map["id"] = pref.getString('uid');

    HttpHelper.post(HttpHelper.API_Version, (data) async {
      //loadpage.close();
      if (data != null) {
        print(data);

        String romot_version = data['VersionName'];
        setState(() {
          version = romot_version;
        });
      }
    }, params: map);
  }

  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        backgroundColor: Colors.white,
        elevation: 0,
        titleSpacing: 0,
        title: Text(
          '设置',
          style: TextStyle(fontSize: Config.title_size),
        ),
      ),
      body: new Stack(
        children: [
          Positioned(
              top: 20,
              left: 20,
              right: 20,
              child: new Column(
                children: [
                  new Center(
                    child: new Image.asset('resource/images/user/logo2.png',
                        height: 200, width: 200),
                  ),
                  new Padding(
                    padding: new EdgeInsets.fromLTRB(0, 10, 0, 10),
                    child: new Center(
                      child: Text(
                        '车魔估',
                        style: TextStyle(fontSize: 20),
                      ),
                    ),
                  ),
                  new Padding(
                    padding: new EdgeInsets.fromLTRB(0, 0, 0, 5),
                    child: new Center(
                      child: Text(
                        "您的版本：" + localVersion,
                        style:
                            TextStyle(fontSize: 13, color: Colours.text_gray),
                      ),
                    ),
                  ),
                  new Padding(
                    padding: new EdgeInsets.fromLTRB(0, 0, 0, 10),
                    child: new Center(
                      child: Text(
                        "最新版本：" + version,
                        style:
                            TextStyle(fontSize: 13, color: Colours.text_gray),
                      ),
                    ),
                  ),
                  new Center(
                    child: Text(
                        '车魔估是一款专注二手车行业服务的应用，通过自主研发的线上“二手车检测系统”（包含300多项检测内容）+“二手车保修系统”结合创新的“共享检测师”模式，立志于改善二手车行业“车况不透明”及“售后没保障”的问题。'),
                  ),
                               localVersion == version
                    ? Container(height: 0)
                    : Padding(
                        padding: new EdgeInsets.fromLTRB(0, 50, 0, 0),
                        child: Row(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                              Padding(
                                padding:
                                    new EdgeInsets.fromLTRB(20, 10, 20, 10),
                                child: Align(
                                    alignment: Alignment.centerRight,
                                    child: new Container(
                                      child: new Material(
                                        //INK可以实现装饰容器
                                        child: new Ink(
                                          //用ink圆角矩形
                                          // color: Colors.red,
                                          decoration: new BoxDecoration(
                                            //背景
                                            color: Colors.white,
                                            //设置四周圆角 角度
                                            borderRadius: BorderRadius.all(
                                                Radius.circular(5.0)),
                                            //设置四周边框
                                            border: new Border.all(
                                                width: 1,
                                                color: Color.fromRGBO(
                                                    153, 153, 153, 1)),
                                          ),
                                          child: new InkWell(
                                              //圆角设置,给水波纹也设置同样的圆角
                                              //如果这里不设置就会出现矩形的水波纹效果
                                              borderRadius:
                                                  new BorderRadius.circular(
                                                      15.0),
                                              //设置点击事件回调
                                              onTap: () async {
                                                checkversion();
                                              },
                                              child: Container(
                                                //设置 child 居中
                                                alignment: Alignment(0, 0),
                                                height: 40,
                                                width: 185,
                                                child: Text(
                                                  "立即更新",
                                                  style: TextStyle(
                                                      color: Color.fromRGBO(
                                                          153, 153, 153, 1)),
                                                ),
                                              )),
                                        ),
                                      ),
                                    )),
                              ),
                            ]),
                      ),
                                   Padding(
                  padding: new EdgeInsets.fromLTRB(0, 10, 0, 30),
                  child: Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        Padding(
                          padding: new EdgeInsets.fromLTRB(20, 10, 20, 10),
                          child: Align(
                              alignment: Alignment.centerRight,
                              child: new Container(
                                child: new Material(
                                  //INK可以实现装饰容器
                                  child: new Ink(
                                    //用ink圆角矩形
                                    // color: Colors.red,
                                    decoration: new BoxDecoration(
                                      //背景
                                      color: Colors.white,
                                      //设置四周圆角 角度
                                      borderRadius: BorderRadius.all(
                                          Radius.circular(5.0)),
                                      //设置四周边框
                                      border: new Border.all(
                                          width: 1,
                                          color:
                                              Color.fromRGBO(153, 153, 153, 1)),
                                    ),
                                    child: new InkWell(
                                        //圆角设置,给水波纹也设置同样的圆角
                                        //如果这里不设置就会出现矩形的水波纹效果
                                        borderRadius:
                                            new BorderRadius.circular(15.0),
                                        //设置点击事件回调
                                        onTap: () async {
                                          SharedPreferences pref =
                                              await SharedPreferences
                                                  .getInstance();
                                          pref.clear();
                                          Navigator.of(context)
                                              .pushAndRemoveUntil(
                                                  new MaterialPageRoute(
                                                      builder: (context) =>
                                                          new MyApp()),
                                                  (route) => route == null);
                                        },
                                        child: Container(
                                          //设置 child 居中
                                          alignment: Alignment(0, 0),
                                          height: 40,
                                          width: 185,
                                          child: Text(
                                            "退出登录",
                                            style: TextStyle(
                                                color: Color.fromRGBO(
                                                    153, 153, 153, 1)),
                                          ),
                                        )),
                                  ),
                                ),
                              )
                           
                              ),
                        )])),
                ],
              )),
          Positioned(
            bottom: 0,
            left: 0,
            right: 0,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                new Center(
                  child: GestureDetector(
                      onTap: () {
                        Navigator.of(context)
                            .push(new MaterialPageRoute(builder: (_) {
                          return new UserPravite(
                              'http://app.chemogu1688.com/app/index.php?i=16&c=entry&m=chemogu&do=mobile&r=car.partnerprivate');
                        }));
                      },
                      child: Center(child: Text('用户隐私政策',style: TextStyle(color: Colours.text_gray),))),
                ),
                Container(height: 5,),
                new Center(
                  child: GestureDetector(
                      onTap: () {
                         Navigator.of(context)
                            .push(new MaterialPageRoute(builder: (_) {
                          return new AgreementPage();
                        }));
                      },
                      child: Center(child: Text('车商入驻协议',style: TextStyle(color: Colours.text_gray),))),
                ),
                Container(
                  height: 5,
                ),
                Text('云南车魔估科技有限公司'),
                Container(
                  height: 5,
                )
              ],
            ),
          )
        ],
      ),
    );
    // return new Scaffold(
    //   appBar: new AppBar(
    //     titleSpacing: 0,
    //     title: new Text(
    //       '设置',
    //       style: TextStyle(fontSize: Config.title_size),
    //     ),
    //     backgroundColor: Colors.white,
    //     elevation: 0,
    //   ),
    //   body: new Stack(
    //     children: <Widget>[
    //       new Padding(
    //         padding: new EdgeInsets.fromLTRB(20.0, 50.0, 20.0, 10.0),
    //         child: new Column(
    //           children: <Widget>[
    //             new Center(
    //               child: new Image.asset('resource/images/user/logo2.png',
    //                   height: 200, width: 200),
    //             ),
    //             new Padding(
    //               padding: new EdgeInsets.fromLTRB(0, 10, 0, 10),
    //               child: new Center(
    //                 child: Text(
    //                   '车魔估',
    //                   style: TextStyle(fontSize: 20),
    //                 ),
    //               ),
    //             ),
    //             new Padding(
    //               padding: new EdgeInsets.fromLTRB(0, 0, 0, 5),
    //               child: new Center(
    //                 child: Text(
    //                   "您的版本：" + localVersion,
    //                   style: TextStyle(fontSize: 13, color: Colours.text_gray),
    //                 ),
    //               ),
    //             ),
    //             new Padding(
    //               padding: new EdgeInsets.fromLTRB(0, 0, 0, 10),
    //               child: new Center(
    //                 child: Text(
    //                   "最新版本：" + version,
    //                   style: TextStyle(fontSize: 13, color: Colours.text_gray),
    //                 ),
    //               ),
    //             ),
    //             new Center(
    //               child: Text(
    //                   '车魔估是一款专注二手车行业服务的应用，通过自主研发的线上“二手车检测系统”（包含300多项检测内容）+“二手车保修系统”结合创新的“共享检测师”模式，立志于改善二手车行业“车况不透明”及“售后没保障”的问题。'),
    //             ),
    //             localVersion == version
    //                 ? Container(height: 0)
    //                 : Padding(
    //                     padding: new EdgeInsets.fromLTRB(0, 50, 0, 0),
    //                     child: Row(
    //                         mainAxisAlignment: MainAxisAlignment.center,
    //                         children: <Widget>[
    //                           Padding(
    //                             padding:
    //                                 new EdgeInsets.fromLTRB(20, 10, 20, 10),
    //                             child: Align(
    //                                 alignment: Alignment.centerRight,
    //                                 child: new Container(
    //                                   child: new Material(
    //                                     //INK可以实现装饰容器
    //                                     child: new Ink(
    //                                       //用ink圆角矩形
    //                                       // color: Colors.red,
    //                                       decoration: new BoxDecoration(
    //                                         //背景
    //                                         color: Colors.white,
    //                                         //设置四周圆角 角度
    //                                         borderRadius: BorderRadius.all(
    //                                             Radius.circular(5.0)),
    //                                         //设置四周边框
    //                                         border: new Border.all(
    //                                             width: 1,
    //                                             color: Color.fromRGBO(
    //                                                 153, 153, 153, 1)),
    //                                       ),
    //                                       child: new InkWell(
    //                                           //圆角设置,给水波纹也设置同样的圆角
    //                                           //如果这里不设置就会出现矩形的水波纹效果
    //                                           borderRadius:
    //                                               new BorderRadius.circular(
    //                                                   15.0),
    //                                           //设置点击事件回调
    //                                           onTap: () async {
    //                                             checkversion();
    //                                           },
    //                                           child: Container(
    //                                             //设置 child 居中
    //                                             alignment: Alignment(0, 0),
    //                                             height: 40,
    //                                             width: 185,
    //                                             child: Text(
    //                                               "立即更新",
    //                                               style: TextStyle(
    //                                                   color: Color.fromRGBO(
    //                                                       153, 153, 153, 1)),
    //                                             ),
    //                                           )),
    //                                     ),
    //                                   ),
    //                                 )),
    //                           ),
    //                         ]),
    //                   ),
    //             Padding(
    //               padding: new EdgeInsets.fromLTRB(0, 10, 0, 30),
    //               child: Row(
    //                   mainAxisAlignment: MainAxisAlignment.center,
    //                   children: <Widget>[
    //                     Padding(
    //                       padding: new EdgeInsets.fromLTRB(20, 10, 20, 10),
    //                       child: Align(
    //                           alignment: Alignment.centerRight,
    //                           child: new Container(
    //                             child: new Material(
    //                               //INK可以实现装饰容器
    //                               child: new Ink(
    //                                 //用ink圆角矩形
    //                                 // color: Colors.red,
    //                                 decoration: new BoxDecoration(
    //                                   //背景
    //                                   color: Colors.white,
    //                                   //设置四周圆角 角度
    //                                   borderRadius: BorderRadius.all(
    //                                       Radius.circular(5.0)),
    //                                   //设置四周边框
    //                                   border: new Border.all(
    //                                       width: 1,
    //                                       color:
    //                                           Color.fromRGBO(153, 153, 153, 1)),
    //                                 ),
    //                                 child: new InkWell(
    //                                     //圆角设置,给水波纹也设置同样的圆角
    //                                     //如果这里不设置就会出现矩形的水波纹效果
    //                                     borderRadius:
    //                                         new BorderRadius.circular(15.0),
    //                                     //设置点击事件回调
    //                                     onTap: () async {
    //                                       SharedPreferences pref =
    //                                           await SharedPreferences
    //                                               .getInstance();
    //                                       pref.clear();
    //                                       Navigator.of(context)
    //                                           .pushAndRemoveUntil(
    //                                               new MaterialPageRoute(
    //                                                   builder: (context) =>
    //                                                       new MyApp()),
    //                                               (route) => route == null);
    //                                     },
    //                                     child: Container(
    //                                       //设置 child 居中
    //                                       alignment: Alignment(0, 0),
    //                                       height: 40,
    //                                       width: 185,
    //                                       child: Text(
    //                                         "退出登录",
    //                                         style: TextStyle(
    //                                             color: Color.fromRGBO(
    //                                                 153, 153, 153, 1)),
    //                                       ),
    //                                     )),
    //                               ),
    //                             ),
    //                           )
    //                           // child: new RaisedButton(
    //                           //   child: Text('申请补考'),
    //                           //   onPressed: () {},
    //                           // ),
    //                           ),
    //                     ),
    //                     // Container(height: 40,width:150 ,child:
    //                     // RaisedButton(
    //                     //   child: Text('退出登录',),
    //                     //   color: Color.fromRGBO(181, 181, 181, 1),
    //                     //   textColor: Colors.white,
    //                     //   onPressed: (){

    //                     //   },
    //                     // ),)
    //                   ]),
    //             ),
    //             Align(

    //                alignment: Alignment.bottomCenter,

    //                 // child: Padding(
    //                 //     padding: new EdgeInsets.fromLTRB(0, 10, 0, 20),
    //                 //     child: new Center(
    //                 //       child: GestureDetector(
    //                 //           onTap: () {
    //                 //             Navigator.of(context)
    //                 //                 .push(new MaterialPageRoute(builder: (_) {
    //                 //               return new UserPravite(
    //                 //                   'http://ylz.9ok6.com/app/index.php?i=16&c=entry&m=chemogu&do=mobile&r=car.partnerprivate');
    //                 //             }));
    //                 //           },
    //                 //           child: Text('用户隐私政策')),
    //                 //     ))
    //                     ),
    //             new Center(
    //               child: Text('技术开发'),
    //             ),
    //             Positioned(
    //                 bottom: 0,
    //                 child: new Center(
    //                   child: Text('云南车魔估科技有限公司'),
    //                 )),
    //           ],
    //         ),
    //       ),
    //     ],
    //   ),
    // );
  }
}
